package com.siyu.tickets.dao.impl;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.siyu.common.hibernate3.Finder;
import com.siyu.common.hibernate3.HibernateBaseDao;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.TicketDao;
import com.siyu.tickets.entity.Ticket;

@Repository
public class TicketDaoImpl extends HibernateBaseDao<Ticket, Integer> implements TicketDao {

	public Pagination getPage(Date beginTime, Date endTime, Boolean valid, int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from Ticket bean");
		f.append(" where 1=1");
		if (valid != null) {
			f.append(" and bean.valid=:valid");
			f.setParam("valid", valid);
		}
		if (beginTime != null) {
			f.append(" and bean.createTime >= :beginTime");
			f.setParam("beginTime", beginTime);
		}
		if (endTime != null) {
			f.append(" and bean.createTime <= :endTime");
			f.setParam("endTime", endTime);
		}
		f.append(" order by bean.createTime asc");
		return find(f, pageNo, pageSize);
	}

	public Ticket findById(Integer id) {
		Ticket entity = get(id);
		return entity;
	}

	public Ticket findByCode(String code) {
		return findUniqueByProperty("code", code);
	}

	public Ticket save(Ticket bean) {
		getSession().save(bean);
		return bean;
	}

	public Ticket deleteById(Integer id) {
		Ticket entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	protected Class<Ticket> getEntityClass() {
		return Ticket.class;
	}

}
